package com.pagepub.expo.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Repository;

import com.pagepub.expo.entity.CorpTemplateSource;

@Repository
public class CorpTemplateSourceDao{

	@Autowired
	@Qualifier("jdbcTemplate")
	private JdbcTemplate template;
	
	@Autowired
	@Qualifier("appSeqDao")
	private AppSeqDao appSeqDao;
	
	private static String tableName="corp_templatesource";
	
	
	private static String QUERY_ALL = "SELECT id,templateId,templateName,xwidth,yheight," +
			"zzoom,urlName,orderby FROM "+tableName;

	private static String QUERY_ALL_COUNT = "SELECT COUNT(*) FROM "+  tableName;
	
	private static String INSERT_SQL = "INSERT  INTO "+tableName+" (" +
			"id,templateId,templateName,xwidth,yheight," +
			"zzoom,urlName,orderby)VALUES(" +
			":id,:templateId,:templateName,:xwidth,:yheight," +
			":zzoom,:urlName,:orderby)";

	private static String DELETE_SQL = "DELETE FROM "+ tableName +" WHERE id=?";

	private static String UPDATE_SQL = "UPDATE "+ tableName + " SET " +
			"templateId=:templateId,templateName=:templateName,xwidth=:xwidth,yheight=:yheight," +
			"zzoom=:zzoom,urlName=:urlName,orderby=:orderby "+
			"WHERE id=:id";

	private final ParameterizedRowMapper<CorpTemplateSource> mapper = new ParameterizedRowMapper<CorpTemplateSource>() {
		public CorpTemplateSource mapRow(ResultSet rs, int rowNum) throws SQLException {
			CorpTemplateSource obj = new CorpTemplateSource();
			obj.setId(rs.getLong("id"));
			obj.setTemplateId(rs.getInt("templateId"));
			obj.setTemplateName(rs.getString("templateName"));
			obj.setXwidth(rs.getInt("xwidth"));
			obj.setYheight(rs.getInt("yheight"));
			obj.setZzoom(rs.getInt("zzoom"));
			obj.setUrlName(rs.getString("urlName"));
			obj.setOrderby(rs.getShort("orderby"));
			return obj;
		}
	};

	public List<CorpTemplateSource> findAll() {
		return template.query(QUERY_ALL, mapper);
	}

	public CorpTemplateSource findById(Long id) {
		List<CorpTemplateSource> list = template.query(QUERY_ALL + " WHERE id=?",
				new Object[] { id }, mapper);
		if (list.size() == 0) {
			return null;
		} else
			return list.get(0);
	}
	
	

	
	public void insert(CorpTemplateSource bean) {
		if(bean.getId()==null){
			bean.setId(new Long(appSeqDao.getSequence("corp_templatesource_seq")));
		}	

		SqlParameterSource ps = new BeanPropertySqlParameterSource(bean);
		NamedParameterJdbcTemplate npTemplate = new NamedParameterJdbcTemplate(
				template.getDataSource());
		npTemplate.update(INSERT_SQL, ps);

	}

	public void delete(Long id) {
		template.update(DELETE_SQL, id);
	}

	public void update(CorpTemplateSource bean) {
		SqlParameterSource ps = new BeanPropertySqlParameterSource(bean);
		NamedParameterJdbcTemplate npTemplate = new NamedParameterJdbcTemplate(
				template.getDataSource());
		npTemplate.update(UPDATE_SQL, ps);
	}
	
	public int getCount(Map<String, String> param){
		int count = template.queryForInt(QUERY_ALL_COUNT);
		return count;
	}
	
	public List findByPage(int currentPage,int pageSize,Map<String, String> param){
		List lists = template.query(QUERY_ALL + " limit ?,?",
				new Object[] { currentPage, pageSize}, mapper);
		return lists;
		
	}
}
